Skip to main content
Version: 1.0.16

CREATE STATISTICS

CREATE STATISTICS — Define extended statistics

Synopsis

CREATE STATISTICS [ IF NOT EXISTS ] statistics_name

[ ( statistics_kind [, ... ] ) ]

ON column_name, column_name [, ...]

FROM table_name

Description

CREATE STATISTICS creates a new extended statistics object, tracking data about the specified table, foreign table, or materialized view. The statistics object will be created in the current database and owned by the user issuing the command.

If a schema name is given (for example, CREATE STATISTICS myschema.mystat ...), the statistics object will be created in the specified schema. Otherwise, it will be created in the current schema. The name of the statistics object must be distinct from that of any other statistics object in the same schema.

Parameters

IF NOT EXISTS

If a statistics object with the same name already exists, no error will be thrown; only a notice will be issued. Note that only the name of the statistics object is considered, not its definition details.

statistics_name

The name of the statistics object to be created (can be schema-qualified).

statistics_kind

The type(s) of statistics to be computed in this statistics object. Currently supported kinds are ndistinct, which enables n-distinct statistics; dependencies, which enables functional dependency statistics; and mcv, which enables most-common-value lists. If this clause is omitted, all supported statistics types will be included in the statistics object.

column_name

The name of a table column to be included in the computed statistics. At least two column names must be provided. The order of column names is not significant.

table_name

The name of the table containing the columns for which statistics are computed (can be schema-qualified).

Note

You must be the owner of a table to create a statistics object that reads from it. However, once created, the statistics object's ownership is independent of the underlying table.

Examples

Create table t1 with two functionally related columns, where the value in the first column is sufficient to determine the value in the other. Then, build functional dependency statistics on these columns:

CREATE TABLE t1 (

a int,

b int

);

INSERT INTO t1 SELECT i/100, i/500

FROM generate_series(1,1000000) s(i);

ANALYZE t1;

-- The number of matching rows will be greatly underestimated:

EXPLAIN ANALYZE SELECT * FROM t1 WHERE (a = 1) AND (b = 0);

CREATE STATISTICS s1 (dependencies) ON a, b FROM t1;

ANALYZE t1;

-- Now the row count estimate will be more accurate:

EXPLAIN ANALYZE SELECT * FROM t1 WHERE (a = 1) AND (b = 0);

# Without functional dependency statistics, the planner assumes that the two WHERE conditions are independent and multiplies their selectivities together, resulting in an underestimated row count. With this statistic, the planner recognizes that the WHERE conditions are redundant and does not underestimate the number of rows.

# Create table t2 with two perfectly correlated columns (containing the same data), and create an MCV list on these columns:

CREATE TABLE t2 (

a int,

b int

);

INSERT INTO t2 SELECT mod(i,100), mod(i,100)

FROM generate_series(1,1000000) s(i);

CREATE STATISTICS s2 (mcv) ON a, b FROM t2;

ANALYZE t2;

-- valid combination (found in MCV)

EXPLAIN ANALYZE SELECT * FROM t2 WHERE (a = 1) AND (b = 1);

-- invalid combination (not found in MCV)

EXPLAIN ANALYZE SELECT * FROM t2 WHERE (a = 1) AND (b = 2);

# The MCV list provides the planner with more detailed information about specific values that commonly appear in the table, as well as an upper bound on selectivity for value combinations not present in the table, allowing it to produce better estimates in both cases.

See Also

ALTER STATISTICS, DROP STATISTICS